<!-- eslint-disable vue/no-parsing-error -->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 第一行：说明和新增 -->
        <el-row>
          <el-col class="el-col1" :span="22"
            ><span class="shuoming"
              >说明：目前支持学科和关键字条件筛选</span
            ></el-col
          >
          <el-col class="el-col1" :span="2">
            <el-button
              type="success"
              size="small"
              @click="$router.push('/questions/new')"
            >
              <i class="el-icon-edit"></i>新增试题
            </el-button>
          </el-col>
        </el-row>
        <el-row>
          <!-- 第二行：筛选 -->
          <el-form ref="form" :model="form">
            <el-col :span="6">
              <el-form-item prop="subjectID" label="学科" label-width="80px">
                <el-select
                  size="small"
                  placeholder="请选择"
                  v-model="form.subjectID"
                  @change="subjects2"
                >
                  <el-option
                    v-for="{ label, value } in subjectsList"
                    :key="value"
                    :label="label"
                    :value="value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                prop="catalogID"
                label="二级目录"
                label-width="80px"
              >
                <el-select
                  ref="aa"
                  size="small"
                  placeholder="请选择"
                  v-model="form.catalogID"
                >
                  <el-option
                    v-for="item in directoryList"
                    :key="item.id"
                    :label="item.directoryName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="tags" label="标签" label-width="80px">
                <el-select
                  size="small"
                  placeholder="请选择"
                  v-model="form.tags"
                >
                  <el-option
                    v-for="item in tagsList"
                    :key="item.id"
                    :label="item.tagName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="keyword" label="关键字" label-width="80px">
                <el-input
                  size="small"
                  placeholder="根据题干搜索"
                  v-model="form.keyword"
                  style="width: 85%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                prop="questionType"
                label="试题类型"
                label-width="80px"
              >
                <el-select
                  size="small"
                  placeholder="请选择"
                  v-model="form.questionType"
                >
                  <el-option
                    v-for="{ value, label } in questionTypeList"
                    :key="value"
                    :value="value"
                    :label="label"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="difficulty" label="难度" label-width="80px">
                <el-select
                  size="small"
                  placeholder="请选择"
                  v-model="form.difficulty"
                >
                  <el-option
                    v-for="{ value, label } in difficultyType"
                    :key="value"
                    :label="label"
                    :value="value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="direction" label="方向" label-width="80px">
                <el-select
                  size="small"
                  placeholder="请选择"
                  v-model="form.direction"
                >
                  <el-option
                    v-for="(value, key) in directionType"
                    :value="value"
                    :key="key"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="creatorID" label="录入者" label-width="80px">
                <el-select
                  size="small"
                  placeholder="请选择"
                  v-model="form.creatorID"
                >
                  <el-option
                    v-for="item in userList"
                    :key="item.id"
                    :value="item.username"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="remarks" label="题目备注" label-width="80px">
                <el-input
                  size="small"
                  v-model="form.remarks"
                  style="width: 85%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                prop="shortName"
                label="企业简称"
                label-width="80px"
              >
                <el-input
                  size="small"
                  v-model="form.shortName"
                  style="width: 85%"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                prop="province"
                label="城市"
                label-width="80px"
                style="display: inline-block; width: 54%"
              >
                <el-select
                  @change="getcitys"
                  placeholder="请选择"
                  size="small"
                  style="width: 100%"
                  v-model="form.province"
                >
                  <el-option
                    v-for="value in provincesList"
                    :key="value"
                    :label="value"
                    :value="value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                prop="city"
                style="display: inline-block; width: 36%"
              >
                <el-select
                  placeholder="请选择"
                  size="small"
                  style="width: 100%"
                  v-model="form.city"
                >
                  <el-option
                    v-for="value in citysList"
                    :key="value"
                    :label="value"
                    :value="value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label-width="163px">
                <el-button size="small" @click="FromCloneClick">清除</el-button>
                <el-button size="small" type="primary" @click="getList"
                  >搜索</el-button
                >
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <el-alert
          type="info"
          show-icon
          :closable="false"
          :title="title"
        ></el-alert>
        <el-table :data="list">
          <el-table-column
            prop="number"
            label="试题编号"
            width="120"
          ></el-table-column>
          <el-table-column prop="subject" label="学科"></el-table-column>
          <el-table-column prop="catalog" label="目录"></el-table-column>
          <el-table-column prop="questionType" label="题型">
            <template #default="{ row }">
              <span>{{ row.questionType === '1' ? '单选' : '多选' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="question" label="题干" width="280">
            <template #default="{ row }">
              <span>{{ row.question }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="addDate"
            label="录入时间"
            width="150"
          ></el-table-column>
          <el-table-column prop="difficulty" label="难度">
            <template #default="{ row }">
              <span>
                {{
                  row.difficulty === '1'
                    ? '简单'
                    : row.questionType === '2'
                    ? '一般'
                    : '困难'
                }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人"></el-table-column>
          <el-table-column label="操作" width="200">
            <template #default="{ row }">
              <el-button
                type="primary"
                plain
                icon="el-icon-view"
                circle
                size="small"
                @click="questPrevClick(row.id)"
              ></el-button>
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                circle
                size="small"
                @click="questionsEnidClick(row.id)"
              ></el-button>
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                circle
                size="small"
                @click="delList(row)"
              ></el-button>
              <el-button
                type="warning"
                plain
                icon="el-icon-check"
                circle
                size="small"
                @click="addChoice(row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :total="counts"
          layout=" prev, pager, next,sizes, jumper"
          :current-page="form.page"
          :page-size="form.pagesize"
          :page-sizes="[5, 10, 20, 50]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </el-card>
    </div>
    <QuestionsPreview
      ref="questPrev"
      :id="questionsPreviewId"
      :dialogVisible.sync="dialogVisible"
    ></QuestionsPreview>
  </div>
</template>

<script>
import QuestionsPreview from '@/module-hmmm/components/questions-preview.vue'
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
import { simple as subjectsSimpleAPI } from '@/api/hmmm/subjects.js'
import { list as directoryListAPI } from '@/api/hmmm/directorys.js'
import { list as tagsListAPI } from '@/api/hmmm/tags.js'
import {
  questionType as topicType,
  difficulty as topicDifficulty,
  direction as topicDirection
} from '@/api/hmmm/constants.js'
import { simple as userSimple } from '@/api/base/users.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
export default {
  data () {
    return {
      // 表格数据
      list: [],
      // 总条数
      counts: 0,
      // 要提交的表单
      form: {
        // 页码
        page: 1,
        // 每页数量
        pagesize: 5,
        // 学科Id
        subjectID: null,
        // 难度
        difficulty: null,
        // 试题类型
        questionType: null,
        // 标签
        tags: null,
        // 城市
        province: '',
        // 区县
        city: '',
        // 关键字
        keyword: '',
        // 题目备注
        remarks: '',
        // 企业简称
        shortName: '',
        // 方向
        direction: '',
        // 录入者
        creatorID: null,
        // 二级目录
        catalogID: null
      },
      // 学科列表
      subjectsList: [],
      // 二级目录
      directoryList: [],
      // 标签
      tagsList: [],
      // 试题类型
      questionTypeList: topicType,
      // 难度
      difficultyType: topicDifficulty,
      // 方向
      directionType: topicDirection,
      // 用户列表
      userList: [],
      // 省份
      provincesList: [],
      // 城市
      citysList: [],
      dialogVisible: false,
      questionsPreviewId: null
    }
  },
  computed: {
    title () {
      return `数据一共${this.counts}条`
    }
  },
  components: {
    QuestionsPreview
  },
  created () {
    this.subjects()
    this.getList()
    this.getUserSimple()
    this.getProvinces()
  },
  methods: {
    // 表格数据
    async getList () {
      const { counts, items } = (await list(this.form)).data
      this.list = items
      this.list.map(item => {
        const dayjs = require('dayjs')
        // 转换时间格式
        item.addDate = dayjs(new Date(item.addDate)).format(
          'YYYY-MM-DD HH:mm:ss'
        )
        // 去除标签
        item.question = item.question.replace(/<[^>]+>/g, '')
      })
      this.counts = counts
    },
    // 删除
    delList (data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(data)
          // 判断是不是当前页的最后一个数据并且不是最后一页
          if (this.list.length === 1 && this.form.page > 1) this.form.page--
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
          // 这里是防止点击取消之后的报错
        })
        .catch(() => 1)
    },
    // 加入精选
    async addChoice (data) {
      data.choiceState = 1
      await choiceAdd(data)
      this.$message.success('加入精选成功')
      this.getList()
    },
    // 学科列表
    async subjects () {
      this.subjectsList = (await subjectsSimpleAPI(this.form)).data
    },
    // 二级学科列表+标签列表
    async subjects2 () {
      this.form.catalogID = null
      this.form.tags = null
      this.directoryList = (await directoryListAPI(this.form)).data.items
      this.tagsList = (await tagsListAPI(this.form)).data.items
    },
    // 录入者列表
    async getUserSimple () {
      this.userList = (await userSimple(this.form)).data
    },
    // 城市列表
    async getProvinces () {
      this.provincesList = await provinces()
    },
    // 区县列表
    async getcitys () {
      this.form.city = ''
      this.citysList = await citys(this.form.province)
      console.log(this.citysList)
    },
    async questPrevClick (id) {
      this.dialogVisible = true
      this.questionsPreviewId = id
      this.$nextTick(() => {
        this.$refs.questPrev.getDetail()
      })
    },
    // 页码切换时
    handleCurrentChange (val) {
      this.form.page = val
      this.getList()
    },
    handleSizeChange (val) {
      this.form.pagesize = val
      this.getList()
    },
    FromCloneClick () {
      this.getList()
      this.$refs.form.resetFields()
    },
    questionsEnidClick (id) {
      this.$router.push({
        path: '/questions/new',
        query: {
          id
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-card {
  .shuoming {
    font-size: 12px;
    color: rgba(243, 142, 142, 0.5);
  }
  .el-col {
    height: 50px;
    line-height: 50px;
  }
  .el-col1 {
    height: 30px;
    line-height: 30px;
    position: relative;
    bottom: 10px;
  }
  .el-alert {
    margin: 10px 0;
  }
  .el-pagination {
    display: flex;
    justify-content: end;
    margin-top: 10px;
  }
  ::v-deep .el-table th.is-leaf {
    border-bottom: 2px solid rgb(231, 231, 231);
    background-color: rgb(250, 250, 250);
    height: 44px;
    line-height: 44px;
  }
}
</style>
